<html>
    
    <head>
        <title>
            Access Through WebProxy - Options
		</title>
		<style type="text/css">
			<!--
			@import url("style.css");
			-->
		</style>

		
		<script language='javascript'>

			function doShowAll() {
				if (localStorage['stored_host']) {
    	            var pairs = '<tr><th>Stored Domains</th></tr>';
	                var stored_host = JSON.parse(localStorage['stored_host'])

                	for (domain in stored_host)
						addRow(domain);
				}
            }


            function delRow(id) {
                var current = window.event.srcElement;
                while ((current = current.parentElement) && current.tagName != 'TR');

                var stored_host = JSON.parse(localStorage['stored_host']);

                delete stored_host[document.getElementById(id).innerHTML];
                localStorage.setItem('stored_host', JSON.stringify(stored_host));

                current.parentElement.removeChild(current);
            }

            function addRow(domain) {

                if (!domain) {
                    domain = document.getElementById('add_domain_text').value;
                }

				if (domain) {
					var stored_host = new Object();
					if (localStorage['stored_host']) {				
						stored_host = JSON.parse(localStorage['stored_host']);		
					}
					
					stored_host[domain] = true;
					localStorage.setItem('stored_host', JSON.stringify(stored_host));
					
                    var tbl = document.getElementById('gradient-style');
                    var lastRow = tbl.rows.length;


                    var iteration = lastRow;


                    if (tbl.rows.length > 0) {
                        iteration = parseInt(tbl.rows[lastRow - 1].cells[0].id) + 1;
                    }

                    var row = tbl.insertRow(lastRow);
                    var cellLeft = row.insertCell(0);
                    cellLeft.id = iteration;
                    var textNode = document.createTextNode(domain);
                    cellLeft.appendChild(textNode);

                    var rmCol = row.insertCell(1);
                    var button = document.createElement("input");
                    button.setAttribute('type', 'button');
                    button.setAttribute('value', 'Remove Domain');
                    button.onclick = new Function("delRow(" + iteration + ")");
                    rmCol.appendChild(button);
                    document.getElementById('add_domain_text').value = '';


                } else {

                    setStatus('<p><strong>Domain is empty!</strong></p>');
                }
            }



            // Saves options to localStorage.
            function save_options() {

                var select = document.getElementById("proxy");
                var proxy = select.value;

				if (proxy) {
                    localStorage["saved_proxy_url"] = proxy;
					setStatus("<p>Saved Proxy URL!</p>")
					restore_options();
                    chrome.browserAction.setTitle({
                        title: "Access through: " + localStorage["saved_proxy_url"]
                    });
                } else {

					setStatus ("<p>Nothing changed! Input is empty</p>");
                }
            }


            // Restores select box state to saved value from localStorage.
            function restore_options() {
                var saved_proxy_url = localStorage["saved_proxy_url"];
				
                document.getElementById("proxy").value = saved_proxy_url;

                var str = "<p>E.g., \"http://ieeexplore.ieee.org/xpl...\" will be rewritten to \"http://ieeexplore.ieee.org.<font color=\"red\">" + saved_proxy_url + "</font>/xpl...\"</p>"

				document.getElementById("current_url").innerHTML = "<p><strong>Currently set to:</strong> " + saved_proxy_url + "</p>" + str;
            }


            function setStatus(msg) {
                var status = document.getElementById('status');
                status.innerHTML = msg;
                setTimeout(function() {
                    status.innerHTML = '';
                }, 1500);
            }
        </script>
    </head>
    
    <body onload="restore_options(); doShowAll()">
		<b id="current_url"> </b>

        <input id="proxy" type="text" size="25">
        <button onclick="save_options()">
            Save
        </button>
		<br/>		
		<br/>		

<hr noshade>
			
		
		Automatically add the proxy url string whenever a page from the following domains to is loaded
		<table id=gradient-style>
	    <thead> 
    		<tr> 
        		<th scope="col" class="rounded-company">Domain</th> 
            	<th scope="col" class="rounded-q1">Click to Remove</th>  
	        </tr> 
    	</thead> 

		</table>

		<input id="add_domain_text" type="text" size="25">		
        <button onclick="addRow()">
            Add Domain
		</button>
		
		<b id="status"></b>
    </body>

</html>

